package com.eastelsoft;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;

import com.eastelsoft.filters.RequestFilter;

@SpringBootApplication
@ComponentScan(basePackages={"com.eastelsoft"})
public class RobotProApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(RobotProApplication.class);
	}
	
	public static void main(String[] args) {
		SpringApplication.run(RobotProApplication.class, args);
	}
	
	@Bean
	public FilterRegistrationBean regRequestFilter() {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		bean.setFilter(new RequestFilter());
		bean.setOrder(1);
		return bean;
	}
	
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {
				container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
				container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
			}
		};
	}
	
//	@Bean
//	public FilterRegistrationBean regLoginFilter() {
//		FilterRegistrationBean bean = new FilterRegistrationBean();
//		bean.setFilter(new LoginFilter());
//		bean.setOrder(2);
//		return bean;
//	}
	
}
